//
//  TSUserListTableView.m
//  chart
//
//  Created by xu on 15/8/16.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSUserListTableView.h"
#import "TSJournalModel.h"
#import "TSCreateScheduleViewController.h"

@implementation TSUserListTableView{
    TSJournalModel *journalModel;
    NSArray *dataArray;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self=[super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableFooterView = [UIView new];
        
        self.dataSource = self;
        self.delegate = self;
        
        journalModel = [[TSJournalModel alloc] init];
        
        [self.header beginRefreshing];
    }
    
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cell_identify = @"cellIdentify";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
    }
    
    if (dataArray && dataArray.count > indexPath.row) {
        TSJournalItemModel *item_model = [dataArray objectAtIndex:indexPath.row];
        cell.textLabel.text = item_model.title;
        [cell.textLabel setFont:FONT(12.f)];
        cell.detailTextLabel.text = [TSTools getJournalStatusTypeString:item_model.runStatus.integerValue];
        [cell.detailTextLabel setFont:FONT(12.f)];
    }
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray) {
        return [dataArray count];
    }
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (dataArray && dataArray.count > indexPath.row) {
        TSJournalItemModel *item_model = [dataArray objectAtIndex:indexPath.row];
        UIViewController *view_controller = [self getViewController];
        if (view_controller) {
            TSCreateScheduleViewController *schedule_vc = [[TSCreateScheduleViewController alloc] init];
            schedule_vc.journalId = item_model.msgId;
            [view_controller.navigationController pushViewController:schedule_vc animated:YES];
        }
    }
}


#pragma mark - requestData

- (void)headerRequestData
{
    WS(weakSelf);
    self.pageIndex = 0;
    journalModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize)};
    [journalModel fetchWithPath:kRequestUrl_Journal_outList_journal completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (journalModel.list.count > 0) {
            strongSelf.footer.hidden = NO;
            strongSelf->dataArray = journalModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

- (void)footerRequestData
{
    WS(weakSelf);
    self.pageIndex++;
    journalModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize)};
    [journalModel fetchWithPath:kRequestUrl_Journal_outList_journal completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        
        if (journalModel.list.count == 0) {
            strongSelf.footer.hidden = YES;
        }
        else{
            strongSelf->dataArray = journalModel.list;
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

@end
